<?php 
defined('RUN_KATANA') or die('Restricted access');
require_once(MODEL_INCLUDE_PATH.'form/Form.php');
require_once(MODEL_INCLUDE_PATH.'form/FormField.php');
class FormController extends ObjectController
{
	/* Forms */ 
	public 	$Forms;
	public 	$CurrentForm;
	public 	$CurrentFormId;
	/* Forms End*/
	
	public function FormController()
	{
		if($_REQUEST['form'] > 0)
		{ self::setCurrentFormId($_REQUEST['form']); }
		if($_GET['form'] > 0)
		{ 
			if(self::getCurrentFormId() > 0)
			{
				$form = self::getCurrentForm();
				if($form->getMailTo() != '')
				{
					$form->Validate();
					if($form->getValid())
					{ $form->sendResponseMail(); }
				}
			}
		}
	}
	
	/* Get Forms */ 
	public function getForms($key='')
	{ 
		self::loadForms();
		return $this->Forms; 
	}
	public function getCurrentForm()
	{ 
		if((!(is_object($this->CurrentForm))) && (self::getCurrentFormId() > 0))
		{ self::loadForms(self::getCurrentFormId()); }
		return $this->CurrentForm; 
	}
	public function getCurrentFormId() { return $this->CurrentFormId; }
	/* Get Forms End */ 
	
	
	/* Load Functions */ 
	function loadForms($id=0)
	{
		if(!self::getItemLoaded('Forms'.$id))
		{
			global $db;
			
			if($id > 0)
			{ $sql = 'SELECT * FROM '.DB_PERFIX.'fm_forms WHERE id='.$id.';'; }
			else
			{ $sql = 'SELECT * FROM '.DB_PERFIX.'fm_forms ORDER BY id ASC;'; }
			
			$result = $db->query($sql);
			$data = $db->fetch($sql, CACHE_TTL);
			
			if(is_array($data))
			{
				foreach($data as $row)
				{
					$this->Forms[$row['id']] = new Form();
					$this->Forms[$row['id']]->setId($row['id']);
					$this->Forms[$row['id']]->setTitle($row['title']);
					$this->Forms[$row['id']]->setMethod($row['method']);
					$this->Forms[$row['id']]->setAction($row['action']);
					$this->Forms[$row['id']]->setMailTo($row['MailTo']);
					$this->Forms[$row['id']]->setColumns($row['columns']);
					$this->Forms[$row['id']]->setHumanCheck($row['humanCheck']);
					$this->Forms[$row['id']]->setActive($row['active']);
					if((self::getCurrentFormId() > 0) && (self::getCurrentFormId() == $row['id']))
					{ $this->CurrentForm = $this->Forms[$row['id']]; }
				}
			}
			
			unset($data);
			self::setItemLoaded('Forms'.$id, true);
		}
	}
}

?>